package utils.easyui;

import collection.UtilCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import string.UtilString;
import utils.easyui.domain.EzTree;

import java.util.*;


/**
 * @author nsl
 *
 */


    /**
    * @ClassName: UtilEzTree
    * @Description: TODO
    * @author 宁少林
    * @date 2016年10月30日
    *
    */
public class UtilEzTree {

	static Logger logger = LoggerFactory.getLogger(UtilEzTree.class.getName());
	
	
	public static List<EzTree> getTreeLeafSet(List<EzTree> treeList){
		Set<String> nodeIdSet=new LinkedHashSet<>();//保存 节点 id
		Set<String> leafIdSet=new HashSet<>();//保存 叶子 id
		List<EzTree> treeLeafList=new ArrayList<EzTree>();
		if(treeList!=null){
			for(EzTree t:treeList){
				//1.有childrenList.size 为nodeMap
				if(t.getChildren()!=null && t.getChildren().size()>0){
					nodeIdSet.add(t.getId());
				}
				if(t.getPid()!=null){
					nodeIdSet.add(t.getPid());
				}
				leafIdSet.add(t.getId());
			}
			leafIdSet.removeAll(nodeIdSet);
		}
		for(EzTree tree:treeList){
			if(leafIdSet.contains(tree.getId())){
				treeLeafList.add(tree);
			}
		}
		return treeLeafList;
	}
	
	/**
	 * 设置tree中的叶子节点为open 状态，其他节点都为close,防止tree 循环
	 * @param treeList
	 * @return
	 */
	public  static void setTreeLeafStateOpen(List<EzTree> treeList){
		List<EzTree> treeLeafList=getTreeLeafSet(treeList);
		//设置叶子 open other=closed
		for(EzTree t:treeList){
			if(treeLeafList.contains(t)){
				t.setState("open");
				//t.setIconCls("tree-file");
			}else {
				//t.setState("closed");
				//t.setIconCls("tree-folder");
			}
		}
	}
	
	public static void setTreeNodeOpenCascade(List<EzTree> treeList,Collection<String> idCollection){
		for(String id:idCollection){
			setTreeNodeOpenCascade(treeList,id);
		}
	}
	
	
	/**
	 * 设置tree中的 指定id 节点及所有父节点 state=open
	 * @param treeList
	 * @return
	 */
	public static void setTreeNodeOpenCascade(List<EzTree> treeList,String id){
		Map<String, EzTree> treeMap=new HashMap<>();//<treeId,tree>
		
		for(EzTree t:treeList){
			treeMap.put(t.getId(), t);
		}
		setTreeNodeOpenCascade(treeMap,id);
	}
	
	
	
	    /**
	    * @Title: setTreeNodeOpenCascade
	    * @Description: 递归设置tree open 
	    * @param @param treeMap <treeId,tree>
	    * @param @param id    
	    * @return void    
	    * @throws
	    */
	public static void setTreeNodeOpenCascade(Map<String,EzTree> treeMap,String id){
		Set<String> idSet=treeMap.keySet();
		if(idSet.contains(id)){
			EzTree tree=treeMap.get(id);
			tree.setState("open");
			if(tree.getPid()!=null){
				setTreeNodeOpenCascade(treeMap,tree.getPid());
			}
		}
	}
	
	/**根据 checkIds 设置 treeList中的tree 状态为checked 状态，并返回list,默认分隔符为:,
	 * @param treeList
	 * @param checkIds
	 * @return
	 */
	static public List<EzTree> setTreeCheckedByIds(List<EzTree> treeList,String checkIds){
		if(UtilString.isNullOrEmpty(checkIds)){
			return treeList;
		}
		String[] idArray=checkIds.split(",");
		List<String> idList=Arrays.asList(idArray);
		return setTreeCheckedByIds(treeList, idList);
	}
	
	/**根据 checkIds 设置 treeList中的tree 状态为checked 状态，并返回list,默认分隔符为:,
	 * @param treeList
	 * @param checkIdList
	 * @return
	 */
	static public List<EzTree> setTreeCheckedByIds(List<EzTree> treeList,List<String> checkIdList){
		if(UtilCollection.isNullOrEmpty(checkIdList)){
			return treeList;
		}
		List<EzTree> leafTreeList=getTreeLeafSet(treeList);
		for(EzTree leaf:leafTreeList){
			if(checkIdList.contains(leaf.getId())){
				leaf.setChecked(true);
			}
		}
		return treeList;
	}
	
	static public void setTreeState(List<EzTree> treeList,boolean isOpen){
		if(isOpen){
			setTreeState(treeList,"open");
		}else {
			setTreeState(treeList,"closed");
		}
	}
	
	static public void setTreeState(List<EzTree> treeList,String state){
		if(UtilCollection.isNullOrEmpty(treeList)){
			return ;
		}
		if(state.trim().equalsIgnoreCase("open")){
			state="open";
			for(EzTree tree:treeList){
				tree.setState(state);;
			}
		} else if(state.trim().equalsIgnoreCase("closed")) {
			state="closed";
			for(EzTree tree:treeList){
				tree.setState(state);;
			}
		}else {
			throw new RuntimeException("state is error");
		}
	}
}
